package org.museum.collection.controller;


import org.common.util.JwtUtils;
import org.common.util.Result;
import org.common.util.ThreadlocalUtils;
import org.museum.collection.domain.pojo.ListTableCollectionDetail;
import org.museum.collection.service.ICollectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 藏品主表 前端控制器
 * </p>
 *
 * @author zafkiel
 * @since 2025-07-11
 */
@RestController
@RequestMapping("/collection")
public class CollectionController {
    @Autowired
    private ICollectionService collectionServiceImpl;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @GetMapping("/getOneCollection/{name}")
    //获取藏品的详细信息
    public Result getCollectionDetail(@PathVariable("name") String name){
        Result result = Result.success();
        ListTableCollectionDetail collection = collectionServiceImpl.getCollectionDetail(name);
        result.put("collection",collection);
        result.put("randomCollection",collectionServiceImpl.getRandomCollection(collection.getSubcategory()));
        return result;
    }

    //判断用户是否点赞
    @GetMapping("/isLikeCollection/{collectionId}")
    public Result isLikeCollection(@PathVariable("collectionId") Integer collectionId){
        System.out.println("用户是否点赞");
        //获取token
        String token = ThreadlocalUtils.get();
        if(token==null|| !JwtUtils.validateJwt(token)){
            return Result.success("用户未登录默认没有点赞");
        }
        String phone = JwtUtils.getJwtUsername(token);
        //进行判断用户是否点赞
        boolean res=collectionServiceImpl.isLikeCollection(phone,collectionId);
        if(!res){
            return Result.success("没有点赞");
        }
        return Result.error("已经点赞过了");
    }
    //进行点赞操作
    @PostMapping("/likeCollection/{collectionId}")
    public Result likeCollection(@PathVariable("collectionId") Integer collectionId){
        String token = ThreadlocalUtils.get();
        if(token==null|| !JwtUtils.validateJwt( token)){
            return new Result(401,"请先登录");
        }
        String phone = JwtUtils.getJwtUsername(token);
        boolean res=collectionServiceImpl.likeCollection(phone,collectionId);
        if(res){
            return Result.success("点赞成功");
        }
        return Result.error("已经点赞过了");
    }

    //进行取消点赞操作
    @PostMapping("/cancelLikeCollection/{collectionId}")
    public Result cancelLikeCollection(@PathVariable("collectionId") Integer collectionId){
        System.out.println("取消点赞");
        String token = ThreadlocalUtils.get();
        if(token==null|| !JwtUtils.validateJwt( token)){
            return new Result(401,"请先登录");
        }
        String phone = JwtUtils.getJwtUsername(token);
        boolean res=collectionServiceImpl.cancelLikeCollection(phone,collectionId);
        System.out.println(res);
        if(res){
            return Result.success("取消点赞成功");
        }
        return Result.error("取消点赞失败");
    }
}
